package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.CfetsNews;
import com.cfets.annualAssessment.mapper.CfetsNewsMapper;
import com.cfets.annualAssessment.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author DavidSun
 * @create 2018-12-07 15:56
 * @desc 应用层
 **/
@Service
public class CfetsNewsService {
    @Autowired
    private CfetsNewsMapper cfetsNewsMapper;


    // 获取所有数据
    public List<CfetsNews> getNews(CfetsNews news) {
        Example example = new Example(CfetsNews.class);
        Example.Criteria criteria = example.createCriteria();
        if (!CheckUtil.isEmpty(news.getHead())) {
            criteria.andEqualTo("head", news.getHead() + "%");
        }
        if (!StringUtils.isEmpty(news.getMess())) {
            criteria.andLike("mess", "%" + news.getMess() + "%");
        }
        if (!StringUtils.isEmpty(news.getPicc())) {
            criteria.andLike("picc", "%" + news.getPicc() + "%");
        }
        if (!StringUtils.isEmpty(news.getAuthor())){
            criteria.andLike("author","%"+news.getAuthor()+"%");
        }
        if (!StringUtils.isEmpty(news.getMedia())){
            criteria.andLike("media","%"+news.getMedia()+"%");
        }
        if (!StringUtils.isEmpty(news.getCfstyle())){
            criteria.andLike("cfstyle","%"+news.getCfstyle()+"%");
        }
//        if (!StringUtils.isEmpty(news.getNum())) {
//            criteria.andEqualTo("num", news.getNum());
//        }

        return cfetsNewsMapper.selectByExample(example);
    }


    // 根据id 来获取点赞数
    public CfetsNews getCfetsNewsbyId(long id) {
        return cfetsNewsMapper.selectByPrimaryKey((int) id);
    }


    // 更新点赞数
    public boolean updateCfetsNews(CfetsNews cfetsNews) {
        return cfetsNewsMapper.updateByPrimaryKey(cfetsNews) > 0;
    }

// public CfetsNews  updateNum(CfetsNews cfetsNews ){
//    if (!StringUtils.isEmpty(cfetsNews.getNum())){
//        cfetsNews.setNum();
//    }
//
// }


}